home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Games / Hexagonal CA / HexCA.c / NewDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-30  |  6.9 KB  |  260 lines  |  [TEXT/CWIE]

  1. #include "AppTypes.h"
  2. #include "AppMacros.h"
  3. #include "CoreDialogUtilPr.h"
  4. #include "AppConstants.h"
  5.  
  6. #include "NewDialogPr.h"
  7.  
  8. static DialogRef        InitNewDialog        ( void );
  9.  
  10. static char        scale = kMiniHexSize;            // what hex size is default?
  11. static Boolean        drawIteration = false;            // do we draw the iteration string or not?
  12. static Boolean        alive[7] = {0, 1, 1, 0, 1, 1, 0};    // the default rule set
  13. static Boolean        dead[7] = {0, 0, 0, 1, 1, 0, 0};
  14. static Boolean        centerSeed = false;                // do we only seed the center or perform a random seed?
  15.  
  16. // doNewDialog -    call to display and handle the New... dialog
  17. Boolean doNewDialog( GridDataPtr refCon, Boolean *seedCenter )
  18. {
  19.     DialogRef            theDlog;
  20.     short            itemHit = 0;
  21.     
  22.     if( !(theDlog = InitNewDialog()) )
  23.         return( false );
  24.             
  25.     while( itemHit != iOK && itemHit != iCancel )
  26.     {
  27.         EventRecord        theEvent;
  28.         Boolean            handled = false;
  29.         
  30.         WaitNextEvent( everyEvent, &theEvent, 0, nil );
  31.         switch( theEvent.what )
  32.         {
  33.             case mouseDown:
  34.                 WindowRef         theWindow;
  35.                 short              where;
  36.                         
  37.                 where = FindWindow( theEvent.where, &theWindow );
  38.                         
  39.                 switch( where )
  40.                 {
  41.                     case inMenuBar:
  42.                         {
  43.                             short         menuID, itemNumber;
  44.                             long            menuResult;
  45.  
  46.                             menuResult = MenuSelect( theEvent.where );
  47.                             menuID = MHiWord( menuResult );
  48.                             itemNumber = MLoWord( menuResult );
  49.                             
  50.                             switch( menuID )
  51.                             {
  52.                                 case kAppleMenuID:
  53.                                     {
  54.                                         Str255        itemName;
  55.     
  56.                                         if( itemNumber == iAboutItem )
  57.                                             Alert( kAboutScreenID, nil );
  58.                                         else
  59.                                         {
  60.                                             GetItem( GetMHandle( kAppleMenuID ), itemNumber, itemName );
  61.                                             OpenDeskAcc( itemName );
  62.                                         }
  63.                                     }
  64.                                     break;
  65.                                 case kFileMenuID:
  66.                                     if( itemNumber == iQuitItem )
  67.                                     {
  68.                                         itemHit = iCancel;
  69.                                         handled = true;
  70.                                     }
  71.                                     break;
  72.                             }
  73.                             
  74.                             HiliteMenu( 0 );
  75.                         }
  76.                         break;
  77.                     case inSysWindow:
  78.                         SystemClick( &theEvent, theWindow );
  79.                         break;    
  80.                     case inDrag:
  81.                         SelectWindow( theWindow );
  82.                         DragWindow( theWindow, theEvent.where, &qd.screenBits.bounds );
  83.                         handled = true;
  84.                         break;
  85.                     case inGoAway:
  86.                         if( TrackGoAway( theWindow, theEvent.where ) )
  87.                         {
  88.                             itemHit = iCancel;
  89.                             handled = true;
  90.                         }
  91.                         break;
  92.                 }
  93.                 break;
  94.             case keyDown:
  95.             case autoKey:
  96.                 {
  97.                     char             c = theEvent.message & charCodeMask;
  98.                     Rect            box;
  99.                     short        type;
  100.                     Handle        item;
  101.                     long            endTicks;
  102.                     
  103.                     if( (c == 'q' || c == 'Q') && theEvent.modifiers & cmdKey )
  104.                     {
  105.                         MenuKey( c );
  106.                         itemHit = iCancel;
  107.                         handled = true;
  108.                         HiliteMenu( 0 );
  109.                     }
  110.                     else if( c == 13 || c == 3 )
  111.                     {        
  112.                         GetDItem( theDlog, iOK, &type, &item, &box );
  113.     
  114.                         HiliteControl( (ControlHandle)item, true );
  115.                         Delay( 8, &endTicks );
  116.                         HiliteControl( (ControlHandle)item, false );
  117.     
  118.                         itemHit = iOK;
  119.                         handled = true;
  120.                     }
  121.                     else if( (c == '.' && theEvent.modifiers & cmdKey) || (c == 0x1B) )
  122.                     {        
  123.                         GetDItem( theDlog, iCancel, &type, &item, &box );
  124.     
  125.                         HiliteControl( (ControlHandle)item, true );
  126.                         Delay( 8, &endTicks );
  127.                         HiliteControl( (ControlHandle)item, false );
  128.     
  129.                         itemHit = iCancel;
  130.                         handled = true;
  131.                     }
  132.                 }
  133.                 break;
  134.         }
  135.         if( !handled && IsDialogEvent( &theEvent ) && DialogSelect( &theEvent, &theDlog, &itemHit ) )
  136.         {
  137.             switch( itemHit )
  138.             {
  139.                 case i16by16:
  140.                     SetItemValue( theDlog, i16by16, true );
  141.                     SetItemValue( theDlog, i8by8, false );
  142.                     SetItemValue( theDlog, i4by4, false );
  143.                     break;
  144.                 case i8by8:
  145.                     SetItemValue( theDlog, i16by16, false );
  146.                     SetItemValue( theDlog, i8by8, true );
  147.                     SetItemValue( theDlog, i4by4, false );
  148.                     break;
  149.                 case i4by4:
  150.                     SetItemValue( theDlog, i16by16, false );
  151.                     SetItemValue( theDlog, i8by8, false );
  152.                     SetItemValue( theDlog, i4by4, true );
  153.                     break;
  154.                 case iIterationNo:
  155.                 case iSurvive0: case iSurvive1: case iSurvive2: case iSurvive3: case iSurvive4: case iSurvive5: case iSurvive6:
  156.                 case iRepro0: case iRepro1: case iRepro2: case iRepro3: case iRepro4: case iRepro5    : case iRepro6:
  157.                     InvertItem( theDlog, itemHit );
  158.                     break;
  159.                 case iSeedCentre:
  160.                     {
  161.                         ModalFilterUPP        dialogFilter;
  162.                     
  163.                         dialogFilter = NewModalFilterProc( DLOGStandardProc );
  164.                         InvertItem( theDlog, iSeedCentre );
  165.                         if( GetItemValue( theDlog, iSeedCentre ) && !GetItemValue( theDlog, iRepro1 ) && (Alert( kSeedRuleID, dialogFilter ) == 1) )
  166.                             SetItemValue( theDlog, iRepro1, true );
  167.                     }
  168.                     break;
  169.             }
  170.         }
  171.     }
  172.     
  173.     if( itemHit == iOK )
  174.     {
  175.         short    count;
  176.         
  177.         if( GetItemValue( theDlog, i16by16 ) ) refCon->scale = kHexSize;
  178.         else if( GetItemValue( theDlog, i8by8 ) ) refCon->scale = kMiniHexSize;
  179.         else if( GetItemValue( theDlog, i4by4 ) ) refCon->scale = kTinyHexSize;
  180.         scale = refCon->scale;
  181.         refCon->drawIteration = GetItemValue( theDlog, iIterationNo );
  182.         drawIteration = refCon->drawIteration;
  183.         *seedCenter = GetItemValue( theDlog, iSeedCentre );
  184.         centerSeed = *seedCenter;
  185.         for( count = 0; count < 7; count++ )
  186.         {
  187.             refCon->alive[count] = GetItemValue( theDlog, iSurvive0 + count );
  188.             alive[count] = refCon->alive[count];
  189.             refCon->dead[count] = GetItemValue( theDlog, iRepro0 + count );
  190.             dead[count] = refCon->dead[count];
  191.         }
  192.     }
  193.     
  194.     DisposeDialog( theDlog );
  195.     
  196.     return( itemHit == iOK );
  197. }
  198.  
  199. // InitNewDialog -    call to create the New... dialog
  200. static DialogRef InitNewDialog( void )
  201. {
  202.     DialogRef        theDlog;
  203.     Rect            globalRect;
  204.     GrafPtr        savePort;
  205.     Point            where;
  206.     short        scrLength, mBarHeight, count;
  207.     
  208.     if( !(theDlog = GetNewDialog( kNewDialogID, nil, (WindowRef)-1L )) )
  209.     {
  210.         Alert( kNotEnoughMemoryID, nil );
  211.         return( nil );
  212.     }
  213.  
  214.     GetPort( &savePort );
  215.     SetPort( theDlog );
  216.     globalRect = theDlog->portRect;
  217.     LocalToGlobal( (Point *)(&globalRect) );
  218.     LocalToGlobal( (Point *)(&globalRect) + 1 );
  219.     SetPort( savePort );
  220.     mBarHeight = GetMBarHeight();
  221.  
  222.     scrLength = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
  223.  
  224.     where.v = mBarHeight + 60;
  225.     where.h = (scrLength - (globalRect.right - globalRect.left)) / 2;
  226.     
  227.     MoveWindow( theDlog, where.h, where.v, false );
  228.     
  229.     switch( scale )
  230.     {
  231.         case kHexSize:
  232.             SetItemValue( theDlog, i16by16, true );
  233.             SetItemValue( theDlog, i8by8, false );
  234.             SetItemValue( theDlog, i4by4, false );
  235.             break;
  236.         case kMiniHexSize:
  237.             SetItemValue( theDlog, i16by16, false );
  238.             SetItemValue( theDlog, i8by8, true );
  239.             SetItemValue( theDlog, i4by4, false );
  240.             break;
  241.         case kTinyHexSize:
  242.             SetItemValue( theDlog, i16by16, false );
  243.             SetItemValue( theDlog, i8by8, false );
  244.             SetItemValue( theDlog, i4by4, true );
  245.             break;
  246.     }
  247.     SetItemValue( theDlog, iSeedCentre, centerSeed );
  248.     SetItemValue( theDlog, iIterationNo, drawIteration );
  249.     for( count = 0; count < 7; count++ )
  250.     {
  251.         SetItemValue( theDlog, iSurvive0 + count, alive[count] );
  252.         SetItemValue( theDlog, iRepro0 + count, dead[count] );
  253.     }
  254.     
  255.     SetItemHandle( theDlog, iOKOutline, pBWButtonOutlineProc );
  256.     
  257.     ShowWindow( theDlog );
  258.     
  259.     return( theDlog );
  260. }